﻿using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command.LambweaninginfoCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
	/// <summary>
	/// 羔羊断奶
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class LambweaninginfoController : ControllerBase
	{
		/// <summary>
		/// 中介者
		/// </summary>
		private readonly IMediator _mediator;
		private readonly ILogger<LambweaninginfoController> _logger;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator"></param>
		public LambweaninginfoController(IMediator mediator, ILogger<LambweaninginfoController> logger)
		{
			_mediator = mediator;
			_logger = logger;
		}
		/// <summary>
		/// 获取羔羊断奶信息
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<PageOutput<LambweaninginfoOutPut>>> GetLambweaninginfos(GetLambweaninginfoCommand command)
		{
			try
			{
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("获取羔羊断奶信息出错" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 羔羊断奶删除
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> DelLambweaninginfos(params string[] eartags)
		{
			try
			{
				var command = new DelLambweaninginfosCommand(eartags);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("羊羔断奶删除出错" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 羔羊断奶添加
		/// </summary>
		/// <param name="lambweaninginfos"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> AddLambweaninginfo(List<Lambweaninginfo> lambweaninginfos)
		{
			try
			{
				return await _mediator.Send(new AddLambweaninginfoCommand(lambweaninginfos));
			}
			catch (Exception ex)
			{
				_logger.LogError("羊羔断奶添加出错" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 反填羔羊断奶
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public async Task<Results<Lambweaninginfo>> DteilLambweaninginfo(string eartag)
		{
			try
			{
				var command = new DteilLambweaninginfoCommand(eartag);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("反填羔羊断奶出错" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 羔羊断奶修改
		/// </summary>
		/// <returns></returns>
		[HttpPut]
		public async Task<Results<int>> UpdateLambweaninginfo(UpdateLambweaninginfoCommand command)
		{
			try
			{
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("羔羊断奶修改出错" + ex.Message);
				throw;
			}
		}
	}
}
